<template>
  <div class="com-container">
    <div class="com-chart" ref="hot_ref"></div>
    <span class="fa fa-chevron-left arr-left" @click="toLeft" :style="comStyle"></span>
    <span class="fa fa-chevron-right arr-right" @click="toRight" :style="comStyle"></span>
    <span class="cat-name" :style="comStyle">{{ catName }}</span>
  </div>
</template>

<script>
import { getHotData } from "../api"
export default {
  name: "Hot",
  data() {
    return {
      chartInstance: null,
      allData: [], // 所有数据
      currIndex: 0, // 当前处于第几个索引值
      fontsize: 0, // 分辨率
    }
  },
  mounted() {
    this.initChart()
    this.getData()
    window.addEventListener("resize", this.adaptChart)
    this.adaptChart()
  },
  destroyed() {
    window.removeEventListener("resize", this.adaptChart)
  },
  methods: {
    // 初始化echarts
    initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.hot_ref, "chalk")
      const option = {
        title: {
          text: "▎ 热销商品的占比",
          left: 20,
          top: 20,
        },
        tooltip: {
          show: true,
          formatter: (arg) => {
            // 自定义提示信息
            let str = "",
              total = 0
            arg.data.children.forEach((item) => {
              total += item.value
            })

            arg.data.children.forEach((item) => {
              console.log(parseInt())
              str += `${item.name}:${parseInt((item.value / total) * 100)}%<br/>`
            })
            return str
          },
        },
        legend: {
          top: "15%",
          icon: "circle",
        },
        series: [
          {
            type: "pie",
            center: ["50%", "60%"], // 饼图处于的位置
            label: {
              show: false, // 默认不显示线和文字
            },
            emphasis: {
              label: {
                show: true, // 处于高亮状态下显示文字
              },
              labelLine: {
                show: false, // 处于高亮状态下不想显示线
              },
            },
          },
        ],
      }
      this.chartInstance.setOption(option)
    },
    // 得到数据
    async getData() {
      const res = await getHotData()
      this.allData = res
      this.updateChart()
    },
    // 更新数据
    updateChart() {
      // 图例的数据
      const legendArr = this.allData[this.currIndex].children.map((item) => item.name)
      // 饼图的数据
      const pieArr = this.allData[this.currIndex].children
      const option = {
        legend: {
          data: legendArr,
        },
        series: [{ data: pieArr }],
      }
      this.chartInstance.setOption(option)
    },
    // 分辨率适配
    adaptChart() {
      // 随着屏幕的大小改变而改变
      this.fontsize = (this.$refs.hot_ref.offsetWidth / 100) * 3.6
      const option = {
        title: {
          textStyle: {
            fontSize: this.fontsize,
          },
        },
        legend: {
          itemWidth: this.fontsize / 2,
          itemHeight: this.fontsize / 2,
          itemGap: this.fontsize / 2,
          textStyle: {
            fontSize: this.fontsize / 2,
          },
        },
        series: [
          {
            radius: this.fontsize * 4.5,
          },
        ],
      }
      this.chartInstance.setOption(option)
      // 调用图表事件 图表才会发生大小
      this.chartInstance.resize()
    },
    toLeft() {
      this.currIndex--
      if (this.currIndex < 0) {
        this.currIndex = this.allData.length - 1
      }
      this.updateChart()
    },
    toRight() {
      this.currIndex++
      if (this.currIndex > this.allData.length - 1) {
        this.currIndex = 0
      }
      this.updateChart()
    },
  },
  computed: {
    catName() {
      if (!this.allData[this.currIndex]) {
        return ""
      } else {
        return this.allData[this.currIndex].name
      }
    },
    comStyle() {
      return {
        fontSize: this.fontsize + "px",
      }
    },
  },
}
</script>
<style lang="less" scoped>
.arr-left {
  position: absolute;
  left: 10%;
  top: 50%;
  transform: translateY(-50%);
  cursor: pointer;
  color: white;
}
.arr-right {
  position: absolute;
  right: 10%;
  top: 50%;
  transform: translateY(-50%);
  cursor: pointer;
  color: white;
}
.cat-name {
  position: absolute;
  left: 80%;
  bottom: 20px;
  color: white;
}
</style>
